


import ij.ImagePlus;
import ij.plugin.filter.PlugInFilter;
import ij.process.ByteProcessor;
import ij.process.ColorProcessor;
import ij.process.ImageProcessor;


public class Sobel2_ implements PlugInFilter
{

    @Override
    public void run(ImageProcessor ip)
    {
        long tempoInicial = System.currentTimeMillis();
        ByteProcessor processador = (ByteProcessor)ip;
        byte [] pixelsLinear = (byte[])processador.getPixels();
        int largura = ip.getWidth();
        int altura = ip.getHeight();
        byte [] pixelsNovo = new byte[pixelsLinear.length];
        for(int i=0; i<pixelsLinear.length; i++)
        {
            if ((i%largura)+1==largura)//ultima coluna
                continue;
            if ((i/largura)+1==altura)//ultima linha
                continue;
            if ((i/largura)==0)//primeira linha
                continue;
            if ((i%largura)==0)//primeira coluna
                continue;
            pixelsNovo[i] = (byte)
                            (
                            Math.sqrt(Math.pow(pixelsLinear[i+1-largura] +2*pixelsLinear[i+1]+pixelsLinear[i+largura+1]-pixelsLinear[i-largura-1]-2*pixelsLinear[i-1]-pixelsLinear[i+largura-1],2))
                                      +
                            Math.sqrt(Math.pow(pixelsLinear[i-1+largura]+ 2*pixelsLinear[i+largura]+pixelsLinear[i+largura+1]-pixelsLinear[i-largura-1]-2*pixelsLinear[i-largura]-pixelsLinear[i-largura+1],2))
                            )
                                      ;
            
            
        }
        processador.setPixels(pixelsNovo);
        long tempoFinal = System.currentTimeMillis();
        System.out.println("RAPIDO "+(tempoFinal-tempoInicial));
    }

    @Override
    public int setup(String arg0, ImagePlus arg1)
    {
        return DOES_8G;
    }

}